package com.cndw.kungfu;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class DbToExcelTools {

	private static String[] config = {"kfsys_goods_mode","goodsId"};
	private static String fileds = "goodsId, name, goodDesc, goodEffect";
	
	
	
	public static Set<Integer> getSet(int type) {
		String sql = "SELECT " + config[1] + " FROM " + config[0];
		Set<Integer> set = new HashSet<Integer>();
		try {
			JDBC.connect(type);
			ResultSet resultSet = JDBC.excute(sql);
			while (resultSet.next()) {
				set.add(resultSet.getInt(1));
			}
			System.out.println(set.size());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
//			JDBC.close();
		}
		return set;
	}
	
	public static List<Map<String, Object>> getFiledData(int type, Set<Integer> ids) {
		String sql = "SELECT " + fileds + " FROM " + config[0] + " WHERE " + config[1] + " IN (" + getCondition(ids) + ")";
		List<Map<String, Object>>  list = new ArrayList<Map<String,Object>>();
		try {
			JDBC.connect(type);
			ResultSet resultSet = JDBC.excute(sql);
			Map<String, Object> map = null;
			while (resultSet.next()) {
				map = new HashMap<String, Object>();
//				map.put(fileds[0], resultSet.getInt(fileds[0]));
//				map.put(fileds[1], resultSet.getString(fileds[1]));
//				map.put(fileds[2], resultSet.getString(fileds[2]));
				list.add(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
//			JDBC.close();
		}
		return list;
	}
	
	public static String getCondition(Set<Integer> ids) {
		StringBuilder con = new StringBuilder();
		for (Integer id : ids) {
			con.append(id).append(",");
		}
		return con.toString().substring(0, con.length() - 1);
	}
}
